﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Agile.Engine.Service
{
    public abstract class ServiceProcess
    {
        /// <summary>
        /// 代理对象
        /// </summary>
        public IClientServiceProxy Proxy { get; set; }
        /// <summary>
        /// 状态：大于0成功
        /// </summary>
        public int Status { get; set; }
        /// <summary>
        /// 消息
        /// </summary>
        public string Message { get; set; }
        /// <summary>
        /// 数据上下文
        /// </summary>
        public string Context { get; set; }
        /// <summary>
        /// 数据源
        /// </summary>
        public string Provider { get; set; }
        /// <summary>
        /// 服务逻辑名称
        /// </summary>
        public string Rule { get; set; }
        /// <summary>
        /// 完成后回调
        /// </summary>
        public Action<bool, object> ComplateCallback { get; set; }

        public bool InvokeService(object args)
        {
            var init = InitializeParam(args);

            var encode = EncodeParam(init);

            var result = Proxy.InvokeService(encode, InvokeCallback);

            var decode = DecodeResult(result);

            return ComplateInvoke(init, decode);
        }

        /// <summary>
        /// 初始化参数信息
        /// </summary>
        /// <param name="model"></param>
        public abstract object InitializeParam(object model);

        /// <summary>
        /// 加密参数
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public abstract string EncodeParam(object param);
        /// <summary>
        /// 解密结果
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public abstract object DecodeResult(string param);
        /// <summary>
        /// 异步回调
        /// </summary>
        /// <param name="ar"></param>
        public abstract void InvokeCallback(IAsyncResult ar);
        /// <summary>
        /// 完成调用
        /// </summary>
        /// <param name="param"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public abstract bool ComplateInvoke(object param, object result);


    }


}
